home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with c code, please help!
- Date: 19 Jan 1996 08:19 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <19JAN199608194071@erich.triumf.ca>
- References: <surgsw-1901960148530001@128.206.206.86> <mikedorman-1901960725380001@205.148.200.150>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <mikedorman-1901960725380001@205.148.200.150>, mikedorman@cedarnet.com (Mike Dorman) writes...
- >In article <surgsw-1901960148530001@128.206.206.86>,
- >surgsw@mizzou1.missouri.edu (Joel Weinstein) wrote:
- >
- >> #include <stdio.h>
- >> main()
- >> {
- >> int i=0;
- >> char word[100], c;
- >
- >word is an array of 100 chars, there are no pointers declared here.
- >change word to this:
- > char *word;
- >and it will be *much* easier.
-
- But it won't work. You now have reserved space for a pointer-to-char, but have
- not reserved any space for it to point to, not have you set it to point
- anywhere meaningful.
-
- >> printf("Enter a word: ");
- >> while( (c = getchar()) != '\n') {
- >> *word = c;
- >here, replace *word (which is a pointer to something...you didn't specify
- >which char in word you wanted it to point to, so it could be pointing to
- >just about anything) with
- > strcat(word, *c);
-
- strcat() requires two strings - word is still an uninitialized pointer, so
- strcat() will write to some unkown location. This probably won't compile,
- since "*c" is invalid - you probably mean "&c", but that's no good either,
- since "c" is a single char, not a 0 terminated string.
-
-
- >Also, when getting string information from the user, you probably
- >shouldn't use a char array (I know it seems to make more sense that way,
- >but soon it'll become clear that it doesn't), but you should use a string
- >pointer, like this:
- > char *word;
- >Now, word is a pointer to a string, which has no specific or limited
- >length. Then you can use the standard functions like strcpy and strcat to
- >work on string pointers.
-
- No - word is an uninitialized pointer, and no space is reserved for it to point
- to - using it as suggested can only lead to disaster, as you write over random
- locations in memory.
-
- >
- >To do it with an array, you need another variable to keep track of how
- >many characters you've already put into word, so you can index the array
- >correctly:
-
- <some (surprisingly!) valid code snipped>
- >
- > puts("You entered: ");
- > puts(*word[0]);
-
- Most people would use:
- puts(word);
- In this case the name of an array acts like a pointer to the first element of
- the array.
-
- >}
- >
- >But, if you did it with a string pointer, it's this much easier:
- >
- >main()
- >{
- > char *word;
-
- Where does "word" point??
-
- >
- > puts("Enter a word: ");
- > while(scanf("%s", word) != 0)
- This will store the input in some random location (perhaps over-writing some of
- the program or operating system :-( )
-
-
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-
-
-
-
-